/*
    KWin - the KDE window manager
    This file is part of the KDE project.

    SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "RDPTester.h"

Q_DECLARE_METATYPE(KRdp::VideoFrame)

namespace KRdp
{

RDPTester::RDPTester():QObject(nullptr)
{
    qRegisterMetaType<KRdp::VideoFrame>();
}

RDPTester::~RDPTester()
{
    
}


void RDPTester::init()
{
    if(!initRDPServer())
    {
        qCritical()<<"Error during init of RDP Sever";
        return;
    }
}

 bool RDPTester::initRDPServer()
 {
    //SSL证书生成
    certificateGenerated = false;
    QProcess sslProcess;
    sslProcess.start(QStringLiteral("openssl"),
                     {
                         QStringLiteral("req"),
                         QStringLiteral("-nodes"),
                         QStringLiteral("-new"),
                         QStringLiteral("-x509"),
                         QStringLiteral("-keyout"),
                         QStringLiteral("/tmp/krdp.key"),
                         QStringLiteral("-out"),
                         QStringLiteral("/tmp/krdp.crt"),
                         QStringLiteral("-days"),
                         QStringLiteral("1"),
                         QStringLiteral("-batch"),
                     });
    sslProcess.waitForFinished();

    certificate = std::filesystem::path("/tmp/krdp.crt");
    certificateKey = std::filesystem::path("/tmp/krdp.key");
    if (!std::filesystem::exists(certificate) || !std::filesystem::exists(certificateKey)) {
        qCritical() << "Could not generate a certificate and no certificate provided. A valid TLS certificate and key should be provided.";
        return false;
    } else {
        qWarning() << "Temporary certificate generated; ready to accept connections.";
        certificateGenerated = true;
    }
    //create server
    server.setAddress(QHostAddress::Any);
    server.setPort(3389);
    server.setUserName("test");
    server.setPassword("test");
    server.setTlsCertificate(certificate);
    server.setTlsCertificateKey(certificateKey);

    //add event connection
     QObject::connect(&server, &KRdp::Server::newSession, [this](KRdp::Session *newSession) {
        // reader.startPlay();
        // QObject::connect(&this->reader, &::VideoThread::frameReceived, newSession, [this, newSession](const KRdp::VideoFrame &frame) {
        //     //printf("VideoThread::frameReceived\n");
        //     newSession->videoStream()->queueFrame(frame);
        // });

        // QObject::connect(this, &KRdp::PortalSession::cursorUpdate, newSession, [this, newSession](const PipeWireCursor &cursor) {
        //     KRdp::Cursor::CursorUpdate update;
        //     update.hotspot = cursor.hotspot;
        //     update.image = cursor.texture;
        //     newSession->cursor()->update(update);
        // });

        // QObject::connect(newSession->videoStream(), &KRdp::VideoStream::enabledChanged, this, [newSession, this]() {
        //     if (newSession->videoStream()->enabled()) {
        //         portalSession->requestStreamingEnable(newSession->videoStream());
        //     } else {
        //         portalSession->requestStreamingDisable(newSession->videoStream());
        //     }
        // });

        // QObject::connect(newSession->videoStream(), &KRdp::VideoStream::requestedFrameRateChanged, portalSession.get(), [newSession, portalSession]() {
        //     portalSession->setVideoFrameRate(newSession->videoStream()->requestedFrameRate());
        // });

        // QObject::connect(newSession->inputHandler(), &KRdp::InputHandler::inputEvent, this, [this](QEvent *event) {
        //     m_backend->virtualPointer()->event(event);
        //     printf("input event %d\n", event->type());
        // });
    });
    
    // Start the server    
    if (!server.start()) {
        qCritical() << "KRDP server start failed.";
        return false;
    }
    
    return true;
 }


} // namespace
